const modulesData = require('../../modules')
const ModuleKeys = require('../model/moduleKeys')
const ModuleMap = require('../model/moduleMap')
const { alignObjectKeys, getObjectItemMaxLength } = require('../util/object')
const { getArrayItemMaxLength } = require('../util/array')
const { completeString } = require('../util/string')

function getModuleKeyMap() {
  
  const moduleKeyMap = {}
  
  let moduleKey = null

  const itemKeyMaxLength = getObjectItemMaxLength(modulesData)
  
  for (let key in modulesData) {
    moduleKey = key.split('.')[0]
    
    if (!moduleKeyMap[moduleKey]) {
      moduleKeyMap[moduleKey] = []
    }
    
    moduleKeyMap[moduleKey].push(
      `${completeString(key, itemKeyMaxLength)} ->  ${ModuleMap[key]}`,
    )
    
  }
  
  return moduleKeyMap
}

function getModuleTreeKeys() {
  
  const moduleKeyMaxLength = getArrayItemMaxLength(ModuleKeys) + 4
  const moduleKeyMap = getModuleKeyMap()
  
  let moduleTreeKeys = []
  
  ModuleKeys.forEach(key => {
    moduleTreeKeys.push({
      value: `${completeString(key, moduleKeyMaxLength)} ->  ${ModuleMap[key]}`,
      children: moduleKeyMap[key]
    })
  })
  
  return moduleTreeKeys
}

module.exports = {
  getModuleKeyMap,
  getModuleTreeKeys
}